package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmNetworkSysFieldExport;
import com.zb.entity.dto.OrderSysFieldExportDto;
import com.zb.entity.dto.OrderSysFieldExportListDto;
import com.zb.mapper.ZjAdmNetworkSysFieldExportMapper;
import com.zb.search.SearchDto;
import com.zb.service.ZjAdmNetworkSysFieldExportService;
import com.zb.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台服务商字段导出表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-03-05
 */
@Service
public class ZjAdmNetworkSysFieldExportServiceImpl extends ServiceImpl<ZjAdmNetworkSysFieldExportMapper, ZjAdmNetworkSysFieldExport> implements ZjAdmNetworkSysFieldExportService {
    @Resource
    private ZjAdmNetworkSysFieldExportMapper admNetworkSysFieldExportMapper;

    // 新增或更改导出字段
    @Override
    public void save(OrderSysFieldExportDto fieldExport) {
        Assert.isNull(admNetworkSysFieldExportMapper.existByName(fieldExport.getId(), fieldExport.getName()), "字段名称已重复：" + fieldExport.getName());
        Assert.isNull(admNetworkSysFieldExportMapper.existByCode(fieldExport.getId(), fieldExport.getName()), "字段代码已重复：" + fieldExport.getCode());
        ZjAdmNetworkSysFieldExport sysFieldExport = new ZjAdmNetworkSysFieldExport();
        BeanUtil.copyProperties(fieldExport, sysFieldExport);
        sysFieldExport.setDisabled(fieldExport.getStatus());
        if (null != sysFieldExport.getId()) {
            this.updateById(sysFieldExport);
            return;
        }
        sysFieldExport.setId(IdWorker.getId());
        this.save(sysFieldExport);
    }

    // 获取字段列表
    @Override
    public Map<String, Object> getList(SearchDto search) {
        PageUtil.setPageLimit(search);
        List<ZjAdmNetworkSysFieldExport> fieldExportList = admNetworkSysFieldExportMapper.getList(search);
        return PageUtil.pageList(search.getPage(),search.getLimit(), admNetworkSysFieldExportMapper.getListCount(search), fieldExportList.stream().map(field -> {
            OrderSysFieldExportListDto exportListDto = new OrderSysFieldExportListDto();
            BeanUtil.copyProperties(field, exportListDto);
            exportListDto.setStatus(field.getDisabled());
            exportListDto.setCreateTime(field.getGmtCreate());
            exportListDto.setUpdateTime(field.getGmtUpdate());
            return exportListDto;
        }).toList());
    }

    // 根据字段ID获取字段详情
    @Override
    public OrderSysFieldExportDto getDetail(Long id) {
        ZjAdmNetworkSysFieldExport export = this.getById(id);
        Assert.notNull(export, "字段参数[ " + id + " ]的数据不存在");
        OrderSysFieldExportDto exportDto = new OrderSysFieldExportDto();
        BeanUtil.copyProperties(export, exportDto);
        exportDto.setStatus(export.getDisabled());
        return exportDto;
    }
}
